﻿using MediumApi.Db;
using MediumApi.Result;
using MediumApi.Service;
using MediumApi.Tools;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.VisualBasic;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using static MediumApi.Param.AppParam;
using static MediumApi.Result.AppResult;

namespace MediumApi.Controllers
{
    /// <summary>
    /// 管理后台控制器
    /// </summary>
    [ApiController]
    [Route("api/[controller]/[action]")]
    public class ConsoleController : BaseController
    {
        private readonly AppDbContext _context;
        private readonly PermissionService _permissionService;


        private readonly ConsoleService _consoleService;
        public ConsoleController(AppDbContext context, PermissionService permissionService, ConsoleService consoleService)
        {
            _context = context;
            _permissionService = permissionService;
            _consoleService = consoleService;
        }
        /// <summary>
        /// 添加管理员
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<OperateResult> AddBaseUser(AddBaseUserParam param)
        {
            if (string.IsNullOrEmpty(param.Id))
            {
                if (await _consoleService.AddBaseUser(param) > 0) return Success();
            }
            else
            {
                if (await _consoleService.UpdateBaseUser(param) > 0) return Success();
                else return Success();
            }
            return Error("此管理員Id有誤");
        }
        /// <summary>
        /// 删除管理员
        /// </summary>
        /// <param name="UserId">管理员ID</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<OperateResult> DelBaseUser(string BaseUserId)
        {
            if (await _consoleService.DelBaseUser(BaseUserId) > 0) return Success();
            return Error();
        }
        /// <summary>
        /// 查询管理员列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [ProducesResponseType(StatusCodes.Status200OK, Type = typeof(PagedResult<SelectBaseUserListResult>))]
        public async Task<OperateResult> SelectBaseUserList(SelectBaseUserListParam param)
        {
            return Success(await _consoleService.SelectBaseUserList(param));
        }
        /// <summary>
        /// 管理员登录
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<OperateResult> Login(LoginParam param)
        {
            var data = await _context.baseUsers.FirstOrDefaultAsync(e => e.UserName == param.UserName && e.PassWord == param.PassWord);
            if (data == null)
            {
                return Error("账号名或登录密码不正确！");
            }
            List<SelectMenuListResult> MenuList = new List<SelectMenuListResult>();
            MenuList = await _permissionService.GetAllMenusAsync(data.RoleId);

            List<SelectMenuListResult> BtnList = await _permissionService.btnGetAllMenusAsync(data.RoleId);

            var token = TokenHelpers.GetTicketWithCache(data.Id, data.RoleId);
            return Success(new { Token = token, MenuList = MenuList, BtnList = BtnList, RoleId = data.RoleId });
        }
        /// <summary>
        /// 上传图片
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<OperateResult> UploadPicture([FromForm] UploadPictureParam param)
        {
            string FileName = Guid.NewGuid().ToString("N");
            var data = await Tools.Tools.AddFlieAsync(param.Picture, FileName, 0, param.Type);
            if (param.Type == 1)
            {
                if (data) return Success(new { FileName = $"{AppSettingHelper.GetConfig("Path")["ServerIP"]}" + $"{AppSettingHelper.GetConfig("Path")["Picture"]}" + FileName + "0" + Path.GetExtension(param.Picture.FileName) });
            }
            if (param.Type == 2)
            {
                if (data) return Success(new { FileName = $"{AppSettingHelper.GetConfig("Path")["ServerIP"]}" + $"{AppSettingHelper.GetConfig("Path")["Video"]}" + FileName + Path.GetExtension(param.Picture.FileName) });
            }
            return Error();
        }



        /// <summary>
        /// 添加用户
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<OperateResult> AddUser(AddUserParam param)
        {
            if (string.IsNullOrEmpty(param.Id))
            {
                if (await _consoleService.AddUser(param) > 0) return Success();
            }
            else
            {
                if (await _consoleService.UpdateUser(param) > 0) return Success();
                else return Success();
            }
            return Error();
        }
        /// <summary>
        /// 删除用户
        /// </summary>
        /// <param name="UserId">用户ID</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<OperateResult> DelUser(string UserId)
        {
            if (await _consoleService.DelUser(UserId) > 0) return Success();
            return Error();
        }
        /// <summary>
        /// 查询用户列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [ProducesResponseType(StatusCodes.Status200OK, Type = typeof(PagedResult<SelectUserListResult>))]
        public async Task<OperateResult> SelectUserList(SelectUserListParam param)
        {
            return Success(await _consoleService.SelectUserList(param));
        }
        /// <summary>
        /// 查询用户
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [ProducesResponseType(StatusCodes.Status200OK, Type = typeof(SelectUserListResult))]
        public async Task<OperateResult> SelectUser(string UserId)
        {
            return Success(await _consoleService.SelectUser(UserId));
        }
    }
}
